﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Web;
    using System.Xml;

    public class ImportCSV
    {
        private string strMsg = "";

        private string buildCSVCategoryXML(PortalSettings PS, string[] CSVlines, string FieldDelimeter, string FieldQualifier, string FieldEscapedQualifier)
        {
            XmlDataDocument document = new XmlDataDocument();
            SettingsController controller = new SettingsController();
            XmlDataDocument document2 = new XmlDataDocument();
            XmlDataDocument document3 = new XmlDataDocument();
            string expression = "";
            int num = -1;
            int num3 = -1;
            int num2 = -1;
            int intPRefCol = -1;
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            NB_Store_SettingsTextInfo info = controller.GetSettingsTextNotCached(PS.PortalId, "categoryCSV.ImportMapping", "None");
            document2.LoadXml(HttpUtility.HtmlDecode(info.SettingText));
            info = controller.GetSettingsTextNotCached(PS.PortalId, "productCSV.ImportMapping", "None");
            document3.LoadXml(HttpUtility.HtmlDecode(info.SettingText));
            string innerText = document3.SelectSingleNode("P/NB_Store_ProductsInfo/ProductRef").InnerText;
            if (innerText.StartsWith("[COL:"))
            {
                intPRefCol = Conversions.ToInteger(Strings.Replace(Strings.Replace(innerText, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            string str4 = document2.SelectSingleNode("NB_Store_CategoriesInfo/Lang").InnerText;
            if (str4.StartsWith("[COL:"))
            {
                num2 = Conversions.ToInteger(Strings.Replace(Strings.Replace(str4, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            string str2 = document2.SelectSingleNode("NB_Store_CategoriesInfo/CategoryName").InnerText;
            if (str2.StartsWith("[COL:"))
            {
                num = Conversions.ToInteger(Strings.Replace(Strings.Replace(str2, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            string str5 = document2.SelectSingleNode("NB_Store_CategoriesInfo/ParentName").InnerText;
            if (str5.StartsWith("[COL:"))
            {
                num3 = Conversions.ToInteger(Strings.Replace(Strings.Replace(str5, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            Hashtable hashtable2 = new Hashtable();
            if (num3 >= 0)
            {
                hashtable2 = this.getCSVcategoryXref(CSVlines, document2.OuterXml, intPRefCol, FieldDelimeter, FieldQualifier, FieldEscapedQualifier);
            }
            Hashtable hashtable = new Hashtable();
            Hashtable hashtable3 = new Hashtable();
            string str3 = "<categories>";
            if (intPRefCol > 0)
            {
                int num5;
                int num6;
                string[] strArray;
                int num9;
                if (num2 > 0)
                {
                    if (intPRefCol > 0)
                    {
                        IEnumerator enumerator;
                        try
                        {
                            enumerator = validLocales.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                string str8 = Conversions.ToString(enumerator.Current);
                                str3 = str3 + "<" + str8 + ">";
                                int upperBound = CSVlines.GetUpperBound(0);
                                num5 = 0;
                                while (num5 <= upperBound)
                                {
                                    if (CSVlines[num5] != "")
                                    {
                                        strArray = this.SplitCustom(CSVlines[num5], FieldDelimeter, FieldQualifier, FieldEscapedQualifier, true);
                                        expression = document2.OuterXml;
                                        int num8 = strArray.GetUpperBound(0);
                                        num6 = 0;
                                        while (num6 <= num8)
                                        {
                                            num9 = num6 + 1;
                                            expression = Strings.Replace(expression, "[COL:" + num9.ToString() + "]", "<![CDATA[" + strArray[num6] + "]]>", 1, -1, CompareMethod.Binary);
                                            num6++;
                                        }
                                        document = new XmlDataDocument();
                                        document.LoadXml(expression);
                                        if (document.SelectSingleNode("NB_Store_CategoriesInfo/Lang").InnerText == str8)
                                        {
                                            str2 = document.SelectSingleNode("NB_Store_CategoriesInfo/CategoryName").InnerText;
                                            str5 = document.SelectSingleNode("NB_Store_CategoriesInfo/ParentName").InnerText;
                                            if (!hashtable.Contains(str2 + "_" + str8))
                                            {
                                                if (hashtable3.Contains(strArray[intPRefCol - 1]))
                                                {
                                                    document.SelectSingleNode("NB_Store_CategoriesInfo/CategoryID").InnerText = Conversions.ToString(hashtable3[strArray[intPRefCol - 1]]);
                                                }
                                                else
                                                {
                                                    num9 = hashtable.Count + 1;
                                                    document.SelectSingleNode("NB_Store_CategoriesInfo/CategoryID").InnerText = num9.ToString();
                                                }
                                                document.SelectSingleNode("NB_Store_CategoriesInfo/PortalID").InnerText = PS.PortalId.ToString();
                                                document.SelectSingleNode("NB_Store_CategoriesInfo/Lang").InnerText = str8;
                                                document.SelectSingleNode("NB_Store_CategoriesInfo/ParentCategoryID").InnerText = "0";
                                                if (document.SelectSingleNode("NB_Store_CategoriesInfo/ListOrder").InnerText == "")
                                                {
                                                    document.SelectSingleNode("NB_Store_CategoriesInfo/ListOrder").InnerText = "1";
                                                }
                                                document.SelectSingleNode("NB_Store_CategoriesInfo/ProductCount").InnerText = "0";
                                                document.SelectSingleNode("NB_Store_CategoriesInfo/CreatedByUser").InnerText = "-1";
                                                document.SelectSingleNode("NB_Store_CategoriesInfo/CreatedDate").InnerText = this.getFormatToday();
                                                if ((num3 >= 0) && (hashtable2[str5] != null))
                                                {
                                                    document.SelectSingleNode("NB_Store_CategoriesInfo/ParentCategoryID").InnerText = Conversions.ToString(hashtable2[str5]);
                                                }
                                                hashtable.Add(str2 + "_" + str8, document.SelectSingleNode("NB_Store_CategoriesInfo/CategoryID").InnerText);
                                                str3 = str3 + document.OuterXml;
                                            }
                                            if (!hashtable3.Contains(strArray[intPRefCol - 1]))
                                            {
                                                hashtable3.Add(strArray[intPRefCol - 1], RuntimeHelpers.GetObjectValue(hashtable[str2 + "_" + str8]));
                                            }
                                        }
                                    }
                                    num5++;
                                }
                                str3 = str3 + "</" + str8 + ">";
                            }
                        }
                        finally
                        {
                            if (enumerator is IDisposable)
                            {
                                (enumerator as IDisposable).Dispose();
                            }
                        }
                    }
                }
                else
                {
                    str3 = str3 + "<" + SharedFunctions.GetCurrentCulture() + ">";
                    int num10 = CSVlines.GetUpperBound(0);
                    for (num5 = 0; num5 <= num10; num5++)
                    {
                        if (CSVlines[num5] != "")
                        {
                            strArray = this.SplitCustom(CSVlines[num5], FieldDelimeter, FieldQualifier, FieldEscapedQualifier, true);
                            expression = document2.OuterXml;
                            int num11 = strArray.GetUpperBound(0);
                            for (num6 = 0; num6 <= num11; num6++)
                            {
                                num9 = num6 + 1;
                                expression = Strings.Replace(expression, "[COL:" + num9.ToString() + "]", "<![CDATA[" + strArray[num6] + "]]>", 1, -1, CompareMethod.Binary);
                            }
                            document = new XmlDataDocument();
                            document.LoadXml(expression);
                            str2 = document.SelectSingleNode("NB_Store_CategoriesInfo/CategoryName").InnerText;
                            str5 = document.SelectSingleNode("NB_Store_CategoriesInfo/ParentName").InnerText;
                            if (!hashtable.Contains(str2))
                            {
                                num9 = hashtable.Count + 1;
                                document.SelectSingleNode("NB_Store_CategoriesInfo/CategoryID").InnerText = num9.ToString();
                                document.SelectSingleNode("NB_Store_CategoriesInfo/PortalID").InnerText = PS.PortalId.ToString();
                                document.SelectSingleNode("NB_Store_CategoriesInfo/Lang").InnerText = SharedFunctions.GetCurrentCulture();
                                document.SelectSingleNode("NB_Store_CategoriesInfo/ParentCategoryID").InnerText = "0";
                                if (document.SelectSingleNode("NB_Store_CategoriesInfo/ListOrder").InnerText == "")
                                {
                                    document.SelectSingleNode("NB_Store_CategoriesInfo/ListOrder").InnerText = "1";
                                }
                                document.SelectSingleNode("NB_Store_CategoriesInfo/ProductCount").InnerText = "0";
                                document.SelectSingleNode("NB_Store_CategoriesInfo/CreatedByUser").InnerText = "-1";
                                document.SelectSingleNode("NB_Store_CategoriesInfo/CreatedDate").InnerText = this.getFormatToday();
                                if ((num3 >= 0) && (hashtable2[str5] != null))
                                {
                                    document.SelectSingleNode("NB_Store_CategoriesInfo/ParentCategoryID").InnerText = Conversions.ToString(hashtable2[str5]);
                                }
                                hashtable.Add(str2, (hashtable.Count + 1).ToString());
                                str3 = str3 + document.OuterXml;
                            }
                        }
                    }
                    str3 = str3 + "</" + SharedFunctions.GetCurrentCulture() + ">";
                }
            }
            else
            {
                this.strMsg = "ERROR - NO Product Ref Column in CSV file.";
            }
            return Strings.Replace(str3 + "</categories>", "[LANG]", SharedFunctions.GetCurrentCulture(), 1, -1, CompareMethod.Binary);
        }

        private string buildCSVProductXML(PortalSettings PS, string[] CSVlines, string FieldDelimeter, string FieldQualifier, string FieldEscapedQualifier)
        {
            XmlDataDocument xmlDoc = new XmlDataDocument();
            SettingsController controller = new SettingsController();
            XmlDataDocument document2 = new XmlDataDocument();
            XmlDataDocument document3 = new XmlDataDocument();
            string expression = "";
            int num = -1;
            int num3 = -1;
            int num2 = -1;
            int intPRefCol = -1;
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            NB_Store_SettingsTextInfo info = controller.GetSettingsTextNotCached(PS.PortalId, "categoryCSV.ImportMapping", "None");
            document2.LoadXml(HttpUtility.HtmlDecode(info.SettingText));
            info = controller.GetSettingsTextNotCached(PS.PortalId, "productCSV.ImportMapping", "None");
            document3.LoadXml(HttpUtility.HtmlDecode(info.SettingText));
            string innerText = document3.SelectSingleNode("P/NB_Store_ProductsInfo/ProductRef").InnerText;
            if (innerText.StartsWith("[COL:"))
            {
                intPRefCol = Conversions.ToInteger(Strings.Replace(Strings.Replace(innerText, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            string str3 = document3.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText;
            if (str3.StartsWith("[COL:"))
            {
                num2 = Conversions.ToInteger(Strings.Replace(Strings.Replace(str3, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            string str2 = document2.SelectSingleNode("NB_Store_CategoriesInfo/CategoryName").InnerText;
            if (str2.StartsWith("[COL:"))
            {
                num = Conversions.ToInteger(Strings.Replace(Strings.Replace(str2, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            string str4 = document2.SelectSingleNode("NB_Store_CategoriesInfo/ParentName").InnerText;
            if (str4.StartsWith("[COL:"))
            {
                num3 = Conversions.ToInteger(Strings.Replace(Strings.Replace(str4, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
            }
            Hashtable catXRef = new Hashtable();
            catXRef = this.getCSVcategoryXref(CSVlines, document2.OuterXml, intPRefCol, FieldDelimeter, FieldQualifier, FieldEscapedQualifier);
            Hashtable hashtable3 = new Hashtable();
            Hashtable hashtable2 = new Hashtable();
            int unqID = 0;
            string str6 = "<products>";
            if (intPRefCol > 0)
            {
                int num5;
                int num6;
                string[] strArray;
                int num10;
                if (num2 > 0)
                {
                    if (intPRefCol > 0)
                    {
                        IEnumerator enumerator;
                        try
                        {
                            enumerator = validLocales.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                string lang = Conversions.ToString(enumerator.Current);
                                unqID = 0;
                                str6 = str6 + "<" + lang + ">";
                                int upperBound = CSVlines.GetUpperBound(0);
                                num5 = 0;
                                while (num5 <= upperBound)
                                {
                                    if (CSVlines[num5] != "")
                                    {
                                        strArray = this.SplitCustom(CSVlines[num5], FieldDelimeter, FieldQualifier, FieldEscapedQualifier, true);
                                        expression = document3.OuterXml;
                                        int num9 = strArray.GetUpperBound(0);
                                        num6 = 0;
                                        while (num6 <= num9)
                                        {
                                            num10 = num6 + 1;
                                            expression = Strings.Replace(expression, "[COL:" + num10.ToString() + "]", "<![CDATA[" + strArray[num6] + "]]>", 1, -1, CompareMethod.Binary);
                                            num6++;
                                        }
                                        xmlDoc = new XmlDataDocument();
                                        xmlDoc.LoadXml(expression);
                                        str3 = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/UnitCost").InnerText;
                                        if (xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText == lang)
                                        {
                                            innerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductRef").InnerText;
                                            if (!hashtable3.Contains(innerText + "_" + lang) & (innerText != ""))
                                            {
                                                try
                                                {
                                                    string str9;
                                                    if (hashtable2.Contains(strArray[intPRefCol - 1]))
                                                    {
                                                        str9 = Conversions.ToString(hashtable2[strArray[intPRefCol - 1]]);
                                                    }
                                                    else
                                                    {
                                                        num10 = hashtable3.Count + 1;
                                                        str9 = num10.ToString();
                                                    }
                                                    xmlDoc = this.GetProductXML(PS.PortalId, str9, xmlDoc, catXRef, ref unqID, lang);
                                                    hashtable3.Add(innerText + "_" + lang, xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText);
                                                    str6 = str6 + xmlDoc.OuterXml;
                                                }
                                                catch (Exception exception1)
                                                {
                                                    ProjectData.SetProjectError(exception1);
                                                    Exception exception = exception1;
                                                    this.strMsg = this.strMsg + "Product Ref: \"" + innerText + "\" failed to import.<br/>";
                                                    ProjectData.ClearProjectError();
                                                }
                                            }
                                            if (!hashtable2.Contains(strArray[intPRefCol - 1]))
                                            {
                                                hashtable2.Add(strArray[intPRefCol - 1], RuntimeHelpers.GetObjectValue(hashtable3[innerText + "_" + lang]));
                                            }
                                        }
                                    }
                                    num5++;
                                }
                                str6 = str6 + "</" + lang + ">";
                                str6 = Strings.Replace(str6, "[LANG]", lang, 1, -1, CompareMethod.Binary);
                            }
                        }
                        finally
                        {
                            if (enumerator is IDisposable)
                            {
                                (enumerator as IDisposable).Dispose();
                            }
                        }
                    }
                }
                else
                {
                    str6 = str6 + "<" + SharedFunctions.GetCurrentCulture() + ">";
                    int num11 = CSVlines.GetUpperBound(0);
                    for (num5 = 0; num5 <= num11; num5++)
                    {
                        if (CSVlines[num5] != "")
                        {
                            strArray = this.SplitCustom(CSVlines[num5], FieldDelimeter, FieldQualifier, FieldEscapedQualifier, true);
                            expression = document3.OuterXml;
                            int num12 = strArray.GetUpperBound(0);
                            for (num6 = 0; num6 <= num12; num6++)
                            {
                                num10 = num6 + 1;
                                expression = Strings.Replace(expression, "[COL:" + num10.ToString() + "]", "<![CDATA[" + strArray[num6] + "]]>", 1, -1, CompareMethod.Binary);
                            }
                            xmlDoc = new XmlDataDocument();
                            xmlDoc.LoadXml(expression);
                            innerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductRef").InnerText;
                            if (!hashtable3.Contains(innerText) & (innerText != ""))
                            {
                                try
                                {
                                    xmlDoc = this.GetProductXML(PS.PortalId, (hashtable3.Count + 1).ToString(), xmlDoc, catXRef, ref unqID, SharedFunctions.GetCurrentCulture());
                                    hashtable3.Add(innerText, xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText);
                                    str6 = str6 + xmlDoc.OuterXml;
                                }
                                catch (Exception exception3)
                                {
                                    ProjectData.SetProjectError(exception3);
                                    Exception exception2 = exception3;
                                    this.strMsg = this.strMsg + "Product Ref: \"" + innerText + "\" failed to import.<br/>";
                                    ProjectData.ClearProjectError();
                                }
                            }
                        }
                    }
                    str6 = Strings.Replace(str6 + "</" + SharedFunctions.GetCurrentCulture() + ">", "[LANG]", SharedFunctions.GetCurrentCulture(), 1, -1, CompareMethod.Binary);
                }
            }
            else
            {
                this.strMsg = "ERROR - NO Product Ref Column in CSV file.";
            }
            return (str6 + "</products>");
        }

        private void DoModelImport(PortalSettings PS, string[] CSVlines, string FieldDelimeter, string FieldQualifier, string FieldEscapedQualifier)
        {
            XmlDataDocument document = new XmlDataDocument();
            SettingsController controller2 = new SettingsController();
            XmlDataDocument document2 = new XmlDataDocument();
            string expression = "";
            int num4 = -1;
            int num2 = -1;
            int num3 = -1;
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            string xml = SharedFunctions.GetStoreSettingText(PS.PortalId, "modelCSV.ImportMapping", "None", true, true);
            NB_Store_ModelInfo model = null;
            NB_Store_ModelInfo objInfo = new NB_Store_ModelInfo();
            ProductController controller = new ProductController();
            int num = 0;
            int num5 = 0;
            if (xml != "")
            {
                document2.LoadXml(xml);
                string innerText = document2.SelectSingleNode("NB_Store_ModelInfo/ModelRef").InnerText;
                if (innerText.StartsWith("[COL:"))
                {
                    num4 = Conversions.ToInteger(Strings.Replace(Strings.Replace(innerText, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
                }
                string str4 = document2.SelectSingleNode("NB_Store_ModelInfo/ModelID").InnerText;
                if (str4.StartsWith("[COL:"))
                {
                    num2 = Conversions.ToInteger(Strings.Replace(Strings.Replace(str4, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
                }
                string str3 = document2.SelectSingleNode("NB_Store_ModelInfo/Lang").InnerText;
                if (str3.StartsWith("[COL:"))
                {
                    num3 = Conversions.ToInteger(Strings.Replace(Strings.Replace(str3, "[COL:", "", 1, -1, CompareMethod.Binary), "]", "", 1, -1, CompareMethod.Binary));
                }
                Hashtable hashtable2 = new Hashtable();
                Hashtable hashtable = new Hashtable();
                if ((num4 > 0) | (num2 > 0))
                {
                    int upperBound = CSVlines.GetUpperBound(0);
                    for (int i = 0; i <= upperBound; i++)
                    {
                        if (CSVlines[i] != "")
                        {
                            string[] strArray = this.SplitCustom(CSVlines[i], FieldDelimeter, FieldQualifier, FieldEscapedQualifier, true);
                            if (num3 > 0)
                            {
                                str3 = strArray[num3 - 1];
                            }
                            model = null;
                            if ((num2 > 0) && Versioned.IsNumeric(strArray[num2 - 1]))
                            {
                                model = controller.GetModel(Conversions.ToInteger(strArray[num2 - 1]), str3);
                            }
                            if ((model == null) & (num4 > 0))
                            {
                                model = controller.GetModelByRef(strArray[num4 - 1], str3);
                            }
                            if (model != null)
                            {
                                XmlDataDocument xmlDoc = new XmlDataDocument();
                                try
                                {
                                    IEnumerator enumerator;
                                    string str6 = XmlUtils.Serialize(model);
                                    xmlDoc.LoadXml(str6);
                                    XmlNodeList list = document2.SelectNodes("NB_Store_ModelInfo/*");
                                    try
                                    {
                                        enumerator = list.GetEnumerator();
                                        while (enumerator.MoveNext())
                                        {
                                            XmlNode current = (XmlNode) enumerator.Current;
                                            if (current.InnerXml.StartsWith("[COL:"))
                                            {
                                                SharedFunctions.ReplaceXMLNode(xmlDoc, "NB_Store_ModelInfo/" + current.Name, current.InnerXml, false);
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        if (enumerator is IDisposable)
                                        {
                                            (enumerator as IDisposable).Dispose();
                                        }
                                    }
                                    expression = xmlDoc.OuterXml;
                                    int num10 = strArray.GetUpperBound(0);
                                    for (int j = 0; j <= num10; j++)
                                    {
                                        expression = Strings.Replace(expression, "[COL:" + ((j + 1)).ToString() + "]", "<![CDATA[" + strArray[j] + "]]>", 1, -1, CompareMethod.Binary);
                                    }
                                    objInfo = (NB_Store_ModelInfo) XmlUtils.Deserialize(Strings.Replace(Strings.Replace(expression, "[LANG]", SharedFunctions.GetCurrentCulture(), 1, -1, CompareMethod.Binary), "[PORTALID]", Conversions.ToString(PS.PortalId), 1, -1, CompareMethod.Binary), objInfo.GetType());
                                    controller.UpdateObjModel(objInfo);
                                    num5++;
                                }
                                catch (Exception exception1)
                                {
                                    ProjectData.SetProjectError(exception1);
                                    SharedFunctions.UpdateLog("MODEL IMPORT ERROR : " + exception1.ToString());
                                    num++;
                                    ProjectData.ClearProjectError();
                                }
                            }
                            else
                            {
                                num++;
                            }
                        }
                    }
                }
                else
                {
                    this.strMsg = "ERROR - NO ModelRef or ModelID CSV Mapping.";
                }
            }
            else
            {
                this.strMsg = "ERROR - NO Model Mapping \"modelCSV.ImportMapping\" Template.";
            }
            this.strMsg = this.strMsg + "Updated: " + num5.ToString() + "<br/>";
            this.strMsg = this.strMsg + "Failed: " + num.ToString() + "<br/>";
        }

        private Hashtable getCSVcategoryXref(string[] strCSVlines, string CatMapping, int intPRefCol, string FieldDelimeter, string FieldQualifier, string FieldEscapedQualifier)
        {
            Hashtable hashtable = new Hashtable();
            string key = "";
            string expression = "";
            Hashtable hashtable3 = new Hashtable();
            int upperBound = strCSVlines.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                if (strCSVlines[i] != "")
                {
                    string[] strArray = this.SplitCustom(strCSVlines[i], FieldDelimeter, FieldQualifier, FieldEscapedQualifier, true);
                    int num5 = strArray.GetUpperBound(0);
                    for (int j = 0; j <= num5; j++)
                    {
                        strArray[j] = Strings.Replace(strArray[j], "\"\"\"\"\"", "", 1, -1, CompareMethod.Binary);
                    }
                    expression = CatMapping;
                    int num6 = strArray.GetUpperBound(0);
                    for (int k = 0; k <= num6; k++)
                    {
                        int num7 = k + 1;
                        expression = Strings.Replace(expression, "[COL:" + num7.ToString() + "]", "<![CDATA[" + strArray[k] + "]]>", 1, -1, CompareMethod.Binary);
                    }
                    XmlDataDocument document = new XmlDataDocument();
                    document.LoadXml(expression);
                    key = document.SelectSingleNode("NB_Store_CategoriesInfo/CategoryName").InnerText;
                    if (!hashtable.Contains(key))
                    {
                        if (hashtable3.Contains(strArray[intPRefCol - 1]) & (strArray[intPRefCol - 1] != ""))
                        {
                            hashtable.Add(key, RuntimeHelpers.GetObjectValue(hashtable3[strArray[intPRefCol - 1]]));
                        }
                        else
                        {
                            hashtable.Add(key, (hashtable.Count + 1).ToString());
                        }
                    }
                    if (!hashtable3.Contains(strArray[intPRefCol - 1]))
                    {
                        hashtable3.Add(strArray[intPRefCol - 1], RuntimeHelpers.GetObjectValue(hashtable[key]));
                    }
                }
            }
            return hashtable;
        }

        private string getFormatToday()
        {
            return (Conversions.ToString(DateAndTime.Year(DateAndTime.Today)) + "-" + DateAndTime.Month(DateAndTime.Today).ToString("00") + "-" + DateAndTime.Day(DateAndTime.Today).ToString("00") + "T00:00:00");
        }

        private XmlDataDocument GetProductXML(int PortalID, string ProductID, XmlDataDocument xmlDoc, Hashtable CatXRef, ref int UnqID, string Lang)
        {
            string innerText;
            XmlNode current;
            IEnumerator enumerator;
            IEnumerator enumerator2;
            IEnumerator enumerator3;
            IEnumerator enumerator4;
            IEnumerator enumerator5;
            xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText = ProductID;
            xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/PortalID").InnerText = PortalID.ToString();
            xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText = Lang;
            XmlNodeList list = xmlDoc.SelectNodes("P/C/NB_Store_ProductCategoryInfo");
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    current = (XmlNode) enumerator.Current;
                    current.SelectSingleNode("ProductID").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText;
                    current.SelectSingleNode("CategoryID").InnerText = Conversions.ToString(CatXRef[xmlDoc.SelectSingleNode("P/C/NB_Store_ProductCategoryInfo/CategoryID").InnerText]);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            list = xmlDoc.SelectNodes("P/M/NB_Store_ModelInfo");
            try
            {
                enumerator2 = list.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    current = (XmlNode) enumerator2.Current;
                    current.SelectSingleNode("ProductID").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText;
                    UnqID++;
                    current.SelectSingleNode("ModelID").InnerText = Conversions.ToString((int) UnqID);
                    current.SelectSingleNode("QtyTransDate").InnerText = this.getFormatToday();
                    current.SelectSingleNode("QtyTrans").InnerText = "0";
                    current.SelectSingleNode("PortalID").InnerText = PortalID.ToString();
                    if (current.SelectSingleNode("ListOrder").InnerText == "")
                    {
                        current.SelectSingleNode("ListOrder").InnerText = "1";
                    }
                    if (current.SelectSingleNode("QtyRemaining").InnerText == "")
                    {
                        current.SelectSingleNode("QtyRemaining").InnerText = "-1";
                    }
                    current.SelectSingleNode("Lang").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText;
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            list = xmlDoc.SelectNodes("P/I/NB_Store_ProductImageInfo");
            try
            {
                enumerator3 = list.GetEnumerator();
                while (enumerator3.MoveNext())
                {
                    current = (XmlNode) enumerator3.Current;
                    current.SelectSingleNode("ProductID").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText;
                    UnqID++;
                    current.SelectSingleNode("ImageID").InnerText = Conversions.ToString((int) UnqID);
                    if (current.SelectSingleNode("ListOrder").InnerText == "")
                    {
                        current.SelectSingleNode("ListOrder").InnerText = "1";
                    }
                    innerText = current.SelectSingleNode("ImagePath").InnerText;
                    try
                    {
                        current.SelectSingleNode("ImagePath").InnerText = Path.GetDirectoryName(innerText) + @"\" + Path.GetFileNameWithoutExtension(innerText) + ".jpg";
                    }
                    catch (Exception exception1)
                    {
                        ProjectData.SetProjectError(exception1);
                        Exception exception = exception1;
                        current.SelectSingleNode("ImagePath").InnerText = "";
                        ProjectData.ClearProjectError();
                    }
                    innerText = current.SelectSingleNode("ImageURL").InnerText;
                    try
                    {
                        current.SelectSingleNode("ImageURL").InnerText = Path.GetDirectoryName(innerText) + @"\" + Path.GetFileNameWithoutExtension(innerText) + ".jpg";
                    }
                    catch (Exception exception3)
                    {
                        ProjectData.SetProjectError(exception3);
                        Exception exception2 = exception3;
                        current.SelectSingleNode("ImageURL").InnerText = "";
                        ProjectData.ClearProjectError();
                    }
                    current.SelectSingleNode("Lang").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText;
                }
            }
            finally
            {
                if (enumerator3 is IDisposable)
                {
                    (enumerator3 as IDisposable).Dispose();
                }
            }
            list = xmlDoc.SelectNodes("P/D/NB_Store_ProductDocInfo");
            try
            {
                enumerator4 = list.GetEnumerator();
                while (enumerator4.MoveNext())
                {
                    current = (XmlNode) enumerator4.Current;
                    current.SelectSingleNode("ProductID").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText;
                    UnqID++;
                    current.SelectSingleNode("DocID").InnerText = Conversions.ToString((int) UnqID);
                    if (current.SelectSingleNode("ListOrder").InnerText == "")
                    {
                        current.SelectSingleNode("ListOrder").InnerText = "1";
                    }
                    innerText = current.SelectSingleNode("DocPath").InnerText;
                    current.SelectSingleNode("FileName").InnerText = Path.GetFileNameWithoutExtension(innerText);
                    current.SelectSingleNode("FileExt").InnerText = Path.GetExtension(innerText);
                    current.SelectSingleNode("Lang").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText;
                }
            }
            finally
            {
                if (enumerator4 is IDisposable)
                {
                    (enumerator4 as IDisposable).Dispose();
                }
            }
            list = xmlDoc.SelectNodes("P/options/O");
            try
            {
                enumerator5 = list.GetEnumerator();
                while (enumerator5.MoveNext())
                {
                    IEnumerator enumerator6;
                    current = (XmlNode) enumerator5.Current;
                    current.SelectSingleNode("NB_Store_OptionInfo/ProductID").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/ProductID").InnerText;
                    UnqID++;
                    int num = UnqID;
                    current.SelectSingleNode("NB_Store_OptionInfo/OptionID").InnerText = Conversions.ToString((int) UnqID);
                    if (current.SelectSingleNode("NB_Store_OptionInfo/ListOrder").InnerText == "")
                    {
                        current.SelectSingleNode("NB_Store_OptionInfo/ListOrder").InnerText = "1";
                    }
                    current.SelectSingleNode("NB_Store_OptionInfo/Lang").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText;
                    XmlNodeList list2 = current.SelectNodes("OV/NB_Store_OptionValueInfo");
                    try
                    {
                        enumerator6 = list2.GetEnumerator();
                        while (enumerator6.MoveNext())
                        {
                            XmlNode node2 = (XmlNode) enumerator6.Current;
                            UnqID++;
                            node2.SelectSingleNode("OptionValueID").InnerText = Conversions.ToString((int) UnqID);
                            node2.SelectSingleNode("OptionID").InnerText = Conversions.ToString(num);
                            if (node2.SelectSingleNode("ListOrder").InnerText == "")
                            {
                                node2.SelectSingleNode("ListOrder").InnerText = "1";
                            }
                            node2.SelectSingleNode("Lang").InnerText = xmlDoc.SelectSingleNode("P/NB_Store_ProductsInfo/Lang").InnerText;
                        }
                    }
                    finally
                    {
                        if (enumerator6 is IDisposable)
                        {
                            (enumerator6 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator5 is IDisposable)
                {
                    (enumerator5 as IDisposable).Dispose();
                }
            }
            return xmlDoc;
        }

        public string ImportCSVModels(PortalSettings PS, string ImportFile, string RowDelimeter = "\r\n", string FieldDelimeter = ",", string FieldQualifier = "\"", string FieldEscapedQualifier = "\"\"")
        {
            this.strMsg = "Completed.<br/><br/>";
            try
            {
                if (RowDelimeter == "")
                {
                    RowDelimeter = "\r\n";
                }
                if (FieldDelimeter == "")
                {
                    FieldDelimeter = ",";
                }
                if (string.IsNullOrEmpty(FieldQualifier))
                {
                    FieldQualifier = "\"";
                }
                if (string.IsNullOrEmpty(FieldEscapedQualifier))
                {
                    FieldEscapedQualifier = "\"\"";
                }
                string cSVdata = "";
                XmlDataDocument document = new XmlDataDocument();
                FileObj obj2 = new FileObj();
                XmlDataDocument document2 = new XmlDataDocument();
                string errInfo = "";
                cSVdata = obj2.GetFileContents(ImportFile, ref errInfo);
                string[] cSVlines = this.SplitCSVdata(PS.PortalId, cSVdata, RowDelimeter, FieldDelimeter, FieldQualifier, FieldEscapedQualifier);
                this.DoModelImport(PS, cSVlines, FieldDelimeter, FieldQualifier, FieldEscapedQualifier);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.strMsg = this.strMsg + exception1.ToString();
                ProjectData.ClearProjectError();
            }
            return this.strMsg;
        }

        public string ImportCSVProducts(PortalSettings PS, string ImportFile, bool UpdateExistingProductsFlg, bool CreateCategoriesFlg, string RowDelimeter = "\r\n", string FieldDelimeter = ",", string FieldQualifier = "\"", string FieldEscapedQualifier = "\"\"")
        {
            this.strMsg = "";
            try
            {
                if (RowDelimeter == "")
                {
                    RowDelimeter = "\r\n";
                }
                if (FieldDelimeter == "")
                {
                    FieldDelimeter = ",";
                }
                if (string.IsNullOrEmpty(FieldQualifier))
                {
                    FieldQualifier = "\"";
                }
                if (string.IsNullOrEmpty(FieldEscapedQualifier))
                {
                    FieldEscapedQualifier = "\"\"";
                }
                string filename = "";
                string cSVdata = "";
                string xml = "";
                string str4 = "";
                XmlDataDocument document = new XmlDataDocument();
                FileObj obj2 = new FileObj();
                XmlDataDocument document2 = new XmlDataDocument();
                string errInfo = "";
                cSVdata = obj2.GetFileContents(ImportFile, ref errInfo);
                string[] cSVlines = this.SplitCSVdata(PS.PortalId, cSVdata, RowDelimeter, FieldDelimeter, FieldQualifier, FieldEscapedQualifier);
                str4 = this.buildCSVCategoryXML(PS, cSVlines, FieldDelimeter, FieldQualifier, FieldEscapedQualifier);
                xml = "<root version=\"1.0\">";
                Hashtable hashtable = new Hashtable();
                xml = ((xml + this.buildCSVProductXML(PS, cSVlines, FieldDelimeter, FieldQualifier, FieldEscapedQualifier)) + str4 + "<shipping></shipping>") + "<tax></tax>" + "</root>";
                document = new XmlDataDocument();
                document.LoadXml(xml);
                filename = Strings.Replace(ImportFile, ".csv", ".xml", 1, -1, CompareMethod.Binary);
                document.Save(filename);
                Import import = new Import();
                this.strMsg = this.strMsg + import.ImportProducts(PS, filename, UpdateExistingProductsFlg, CreateCategoriesFlg);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.strMsg = this.strMsg + exception1.ToString();
                ProjectData.ClearProjectError();
            }
            return this.strMsg;
        }

        private string JoinCustom(string[] CSVLines, string FieldDelimiter, string FieldQualifier, string FieldEscapedQualifier)
        {
            StringBuilder builder = new StringBuilder();
            foreach (string str2 in CSVLines)
            {
                if (str2 == null)
                {
                    builder = builder.Append(string.Format("{2}{0}{2}{1}", "", FieldDelimiter, FieldQualifier));
                }
                else
                {
                    str2 = str2.Replace(FieldQualifier, FieldEscapedQualifier);
                    builder = builder.Append(string.Format("{2}{0}{2}{1}", str2, FieldDelimiter, FieldQualifier));
                }
            }
            return builder.ToString().TrimEnd(new char[] { Conversions.ToChar(FieldDelimiter) });
        }

        private string[] SplitCSVdata(int PortalID, string CSVdata, string RowDelimeter, string FieldDelimeter, string FieldQualifier, string FieldEscapedQualifier)
        {
            XmlDataDocument document = new XmlDataDocument();
            SettingsController controller = new SettingsController();
            string[] strArray = Strings.Split(CSVdata, RowDelimeter, -1, CompareMethod.Binary);
            try
            {
                IEnumerator enumerator;
                NB_Store_SettingsTextInfo info = controller.GetSettingsTextNotCached(PortalID, "replaceCSV.ImportMapping", "None");
                if (info == null)
                {
                    return strArray;
                }
                string expression = "";
                string find = "";
                string replacement = "";
                bool flag = false;
                document.LoadXml(HttpUtility.HtmlDecode(info.SettingText));
                XmlNodeList list = document.SelectNodes("root/item");
                try
                {
                    enumerator = list.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        XmlNode current = (XmlNode) enumerator.Current;
                        expression = current.Attributes[0].InnerText;
                        find = current.Attributes[1].InnerText;
                        replacement = current.Attributes[2].InnerText;
                        flag = Conversions.ToBoolean(current.Attributes[3].InnerText);
                        if (find != "")
                        {
                            int upperBound = strArray.GetUpperBound(0);
                            for (int i = 0; i <= upperBound; i++)
                            {
                                string[] cSVLines = this.SplitCustom(strArray[i], FieldDelimeter, FieldQualifier, FieldEscapedQualifier, true);
                                int num4 = cSVLines.GetUpperBound(0);
                                for (int j = 0; j <= num4; j++)
                                {
                                    if (!Versioned.IsNumeric(expression))
                                    {
                                        if (flag)
                                        {
                                            cSVLines[j] = Strings.Replace(cSVLines[j], find, replacement, 1, -1, CompareMethod.Binary);
                                        }
                                        else if (cSVLines[j] == find)
                                        {
                                            cSVLines[j] = Strings.Replace(cSVLines[j], find, replacement, 1, -1, CompareMethod.Binary);
                                        }
                                    }
                                    else if ((j + 1) == Conversions.ToInteger(expression))
                                    {
                                        if (flag)
                                        {
                                            cSVLines[j] = Strings.Replace(cSVLines[j], find, replacement, 1, -1, CompareMethod.Binary);
                                        }
                                        else if (cSVLines[j] == find)
                                        {
                                            cSVLines[j] = Strings.Replace(cSVLines[j], find, replacement, 1, -1, CompareMethod.Binary);
                                        }
                                    }
                                }
                                strArray[i] = this.JoinCustom(cSVLines, FieldDelimeter, FieldQualifier, FieldEscapedQualifier);
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.strMsg = this.strMsg + "replaceCSV Error: " + exception1.ToString();
                ProjectData.ClearProjectError();
            }
            return strArray;
        }

        private string[] SplitCustom(string Expression, string Delimiter, string Qualifier, string EscapedQualifier, bool IgnoreCase)
        {
            bool flag = false;
            int startIndex = 0;
            ArrayList list = new ArrayList();
            string str = "";
            char ch = Convert.ToChar(7);
            if (!string.IsNullOrEmpty(EscapedQualifier))
            {
                string[] strArray3 = Expression.Split(new char[] { Conversions.ToChar(Delimiter) });
                Expression = "";
                int upperBound = strArray3.GetUpperBound(0);
                for (int j = 0; j <= upperBound; j++)
                {
                    if (strArray3[j] == EscapedQualifier)
                    {
                        Expression = Expression + strArray3[j] + Delimiter;
                    }
                    else
                    {
                        Expression = Expression + strArray3[j].Replace(EscapedQualifier, Conversions.ToString(ch)) + Delimiter;
                    }
                }
                Expression = Expression.TrimEnd(new char[] { Conversions.ToChar(Delimiter) });
            }
            int num5 = Expression.Length - 1;
            for (int i = 0; i <= num5; i++)
            {
                if ((Qualifier != null) && (string.Compare(Expression.Substring(i, Qualifier.Length), Qualifier, IgnoreCase) == 0))
                {
                    flag = !flag;
                }
                else if ((!flag && (Delimiter != null)) && (string.Compare(Expression.Substring(i, Delimiter.Length), Delimiter, IgnoreCase) == 0))
                {
                    str = Expression.Substring(startIndex, i - startIndex).Trim(new char[] { Conversions.ToChar(Qualifier) }).Replace(Conversions.ToString(ch), Qualifier);
                    list.Add(str);
                    startIndex = i + 1;
                }
            }
            if (startIndex < Expression.Length)
            {
                str = Expression.Substring(startIndex, Expression.Length - startIndex).Trim(new char[] { Conversions.ToChar(Qualifier) }).Replace(Conversions.ToString(ch), Qualifier);
                list.Add(str);
            }
            string[] array = new string[(list.Count - 1) + 1];
            list.CopyTo(array);
            return array;
        }
    }
}

